package com.gtr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gtr.entity.OrderDetail;
import com.gtr.entity.OrderMaster;
import com.gtr.entity.VaccinumInfo;
import com.gtr.feign.VaccinumFeign;
import com.gtr.form.BuyerOrderDetailForm;
import com.gtr.form.BuyerOrderForm;
import com.gtr.mapper.OrderDetailMapper;
import com.gtr.mapper.OrderMasterMapper;
import com.gtr.service.OrderMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gtr.vo.BuyerOrderDetailVO;
import com.gtr.vo.BuyerOrderMasterVO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author gtr
 * @since 2023-02-22
 */
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements OrderMasterService {

    @Autowired
    private VaccinumFeign vaccinumFeign;

    @Autowired
    private OrderMasterMapper orderMasterMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

//    @Autowired
//    private WebSocket webSocket;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    @Transactional
    public String create(BuyerOrderForm buyerOrderForm) { //先存主表OrderMaster再存从表OrderDetail
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setBuyerName(buyerOrderForm.getName());
        orderMaster.setBuyerPhone(buyerOrderForm.getPhone());
        orderMaster.setBuyerInformation(buyerOrderForm.getInformation());
        orderMaster.setBuyerOpenid(buyerOrderForm.getId()); //用户的id，与预约订单的id不同。
        List<BuyerOrderDetailForm> items = buyerOrderForm.getItems();
        BigDecimal orderAmount = new BigDecimal(0);
        for(BuyerOrderDetailForm item : items){
            Integer vaccinumId = item.getVaccinumId();
            Integer vaccinumQuantity = item.getVaccinumQuantity();
            //通过id查价格
            BigDecimal price = this.vaccinumFeign.findPriceById(vaccinumId);
            BigDecimal amount = price.multiply(new BigDecimal(vaccinumQuantity));
            orderAmount = orderAmount.add(amount);
        }
        orderMaster.setOrderAmount(orderAmount); //得出总价
        orderMaster.setOrderStatus(0); //预约订单状态初始化
        orderMaster.setPayStatus(0); //预约订单支付状态
        this.orderMasterMapper.insert(orderMaster);

        //存从表
        for (BuyerOrderDetailForm item : items) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderMaster.getOrderId());
            orderDetail.setVaccinumId(item.getVaccinumId());
            orderDetail.setVaccinumQuantity(item.getVaccinumQuantity());
            VaccinumInfo vaccinumInfo = this.vaccinumFeign.findById(item.getVaccinumId());
            BeanUtils.copyProperties(vaccinumInfo, orderDetail);
            this.orderDetailMapper.insert(orderDetail);
            //减库存
            this.vaccinumFeign.subStockById(item.getVaccinumId(), item.getVaccinumQuantity());
        }
//        this.webSocket.sendMessage("有新的预约");
        this.rocketMQTemplate.convertAndSend("myTopic","有新的预约");
        return orderMaster.getOrderId();
    }

    @Override
    public BuyerOrderMasterVO detail(Integer buyerId, String orderId) {
        QueryWrapper<OrderMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("buyer_openid", buyerId);
        queryWrapper.eq("order_id", orderId);
        OrderMaster orderMaster = this.orderMasterMapper.selectOne(queryWrapper);
        BuyerOrderMasterVO orderMasterVO = new BuyerOrderMasterVO();
        BeanUtils.copyProperties(orderMaster, orderMasterVO);
        QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("order_id", orderId);
        List<OrderDetail> orderDetailList = this.orderDetailMapper.selectList(queryWrapper1);
        List<BuyerOrderDetailVO> buyerOrderDetailVOList = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetailList) {
            BuyerOrderDetailVO detailVO = new BuyerOrderDetailVO();
            BeanUtils.copyProperties(orderDetail, detailVO);
            buyerOrderDetailVOList.add(detailVO);
        }
        orderMasterVO.setOrderDetailList(buyerOrderDetailVOList);
        return orderMasterVO;
    }

    @Override
    @Transactional
    public boolean cancel(Integer buyerId, String orderId) { //取消预约，加疫苗库存，改订单状态
        //加库存
        QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        List<OrderDetail> orderDetailList = this.orderDetailMapper.selectList(queryWrapper);
        for (OrderDetail orderDetail : orderDetailList) {
            this.vaccinumFeign.addStockById(orderDetail.getVaccinumId(), orderDetail.getVaccinumQuantity());
        }
        if(buyerId == null) { //没有buyerId
            return this.orderMasterMapper.cancel2(orderId);
        } else { //有buyerId
            return this.orderMasterMapper.cancel(buyerId, orderId);
        }
    }

    @Override
    public boolean finish(String orderId) {
        return this.orderMasterMapper.finish(orderId);
    }

    @Override
    public boolean pay(Integer buyerId, String orderId) {
        return this.orderMasterMapper.pay(buyerId, orderId);
    }
}
